package com.newland.mesdk.demo.fragment;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

import com.newland.mesdk.demo.main.R;
import com.newland.mtype.module.common.emv.AIDConfig;
import com.newland.mtype.module.common.emv.CAPublicKey;
import com.newland.mtype.module.common.emv.EmvCardInfo;
import com.newland.mtype.module.common.emv.PbocTransLog;
import com.newland.mtype.util.Dump;
import com.newland.mtype.util.ISOUtils;
import com.newland.mesdk.demo.interfaceimpl.EmvInterfaceImpl;
import com.newland.mesdk.demo.interfaceimpl.K21EmvInterfaceImpl;
import com.newland.mesdk.demo.main.MainActivity;
import com.newland.mesdk.demo.util.Const;
import com.newland.mesdk.demo.util.SharedPreferencesUtil;
import com.newland.mesdk.demo.util.Const.MessageTag;

/**
 * Created by YJF EMV模块
 */
public class EMVFragment extends Fragment implements OnClickListener {
	private MainActivity mainActivity;
	private String connectType;
	private String deviceType;
	private EmvInterfaceImpl emvInterfaceImpl;
	private K21EmvInterfaceImpl k21EmvInterfaceImpl;
	private byte[] rid = new byte[] { (byte) 0xA0, 0x00, 0x00, 0x03, 0x33 };
	private byte[] aid = new byte[] { (byte) 0xA0, 0x00, 0x00, 0x03, 0x33, 0x01, 0x01,0x02 };
	private Button btnAddAID;
	private Button btnAddPk, btnGetAllAID, btnGetAllPk;
	private Button btnDeltOneAID, btnDelOnePK, btnClearAllAID;
	private Button btnClearAllPK, btnGetAccoutMess, btnGetPbocLog;
	private int keyIndex = 87;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_emv, null);
		mainActivity = (MainActivity) getActivity();
		connectType = SharedPreferencesUtil.getStringParam(view.getContext(), Const.CONNECTTYPE);
		deviceType = SharedPreferencesUtil.getStringParam(view.getContext(), Const.DEVICETYPE);
		initEMVFragment(view);
		return view;
	}

	public void initEMVFragment(View view) {
		btnAddAID = (Button) view.findViewById(R.id.btn_emv_addaid);
		btnAddAID.setOnClickListener(this);

		btnAddPk = (Button) view.findViewById(R.id.btn_emv_addpk);
		btnAddPk.setOnClickListener(this);

		btnGetAllAID = (Button) view.findViewById(R.id.btn_emv_get_allaid);
		btnGetAllAID.setOnClickListener(this);

		btnGetAllPk = (Button) view.findViewById(R.id.btn_emv_get_allpk);
		btnGetAllPk.setOnClickListener(this);

		btnGetAccoutMess = (Button) view.findViewById(R.id.btn_emv_get_acount_mess);
		btnGetAccoutMess.setOnClickListener(this);

		btnDeltOneAID = (Button) view.findViewById(R.id.btn_emv_delt_one_aid);
		btnDeltOneAID.setOnClickListener(this);

		btnDelOnePK = (Button) view.findViewById(R.id.btn_emv_delt_one_pk);
		btnDelOnePK.setOnClickListener(this);

		btnClearAllAID = (Button) view.findViewById(R.id.btn_emv_clear_all_aid);
		btnClearAllAID.setOnClickListener(this);

		btnClearAllPK = (Button) view.findViewById(R.id.btn_emv_clear_all_pk);
		btnClearAllPK.setOnClickListener(this);

		btnGetPbocLog = (Button) view.findViewById(R.id.btn_emv_get_pboc_log);
		btnGetPbocLog.setOnClickListener(this);

		if (!connectType.equals(Const.ConnectTypeName.K21)) {
			btnGetPbocLog.setVisibility(View.GONE);
			btnGetAccoutMess.setVisibility(View.GONE);
		}
	}

	@Override
	public void onClick(View v) {
		if (mainActivity.isDeviceInit()) {
			if (mainActivity.processingisLocked()) {
				mainActivity.showMessage("请先完成或撤销当前操作...", MessageTag.ERROR);
			} else {
				mainActivity.connectDevice();
				k21EmvInterfaceImpl = new K21EmvInterfaceImpl();
				emvInterfaceImpl = new EmvInterfaceImpl();
				switch (v.getId()) {
				case R.id.btn_emv_addaid:
					new Thread(new Runnable() {

						@Override
						public void run() {
							try {
								mainActivity.processingLock();
								mainActivity.showMessage("开始增加AID...", MessageTag.TIP);
								AIDConfig aidConfig = new AIDConfig();
								aidConfig.setAid(ISOUtils.hex2byte("A000000333010102"));// 0x9f06
								aidConfig.setAppSelectIndicator(0);// 0xDF01
								aidConfig.setAppVersionNumberTerminal(new byte[] { 0x00, (byte) 0x20 });// 0x9f09
								aidConfig.setTacDefault(ISOUtils.hex2byte("FC78FCF8F0"));// 0xDF11
								aidConfig.setTacOnLine(ISOUtils.hex2byte("FC78FCF8F0"));// 0xDF12
								aidConfig.setTacDenial(ISOUtils.hex2byte("0010000000"));// 0xDF13
								aidConfig.setTerminalFloorLimit(new byte[] { 0x00, 0x00, 0x00, 0x05 });// 0x9f1b
								aidConfig.setThresholdValueForBiasedRandomSelection(new byte[] { 0x00, 0x00, 0x00,
										(byte) 0x28 });// 0xDF15
								aidConfig.setMaxTargetPercentageForBiasedRandomSelection(32);// 0xDF16
								aidConfig.setTargetPercentageForRandomSelection(14);// 0xDF17
								aidConfig.setDefaultDDOL(ISOUtils.hex2byte("9F3704"));// 0xDF14
								aidConfig.setOnLinePinCapability(1);// 0xDF18
								aidConfig.setEcTransLimit(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 });// 0x9F7B
								aidConfig.setNciccOffLineFloorLimit(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 });// 0xDF19
								aidConfig.setNciccTransLimit(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 });// 0xDF20
								aidConfig.setNciccCVMLimit(new byte[] { 0x00, 0x00, 0x00, 0x00, (byte) 0x80, 0x00 });// 0xDF21
								aidConfig.setEcCapability(0);// 0xDF24
								aidConfig.setCoreConfigType(2);// 0xDF25
								boolean addAIDResult1 = false;
								if (connectType.equals(Const.ConnectTypeName.K21)) {
									k21EmvInterfaceImpl.initEmvModule(mainActivity);
									addAIDResult1 = k21EmvInterfaceImpl.addAID(aidConfig);
								} else {
									addAIDResult1 = emvInterfaceImpl.addAID(aidConfig);
								}
								mainActivity.showMessage("添加AID结果:" + addAIDResult1, MessageTag.DATA);

								AIDConfig aidConfig2 = new AIDConfig();
								aidConfig2.setAid(ISOUtils.hex2byte("A000000333010101"));// 0x9f06
								aidConfig2.setAppSelectIndicator(0);// 0xDF01
								aidConfig2.setAppVersionNumberTerminal(new byte[] { 0x00, (byte) 0x20 });// 0x9f09
								aidConfig2.setTacDefault(ISOUtils.hex2byte("FC78FCF8F0"));// 0xDF11
								aidConfig2.setTacOnLine(ISOUtils.hex2byte("FC78FCF8F0"));// 0xDF12
								aidConfig2.setTacDenial(ISOUtils.hex2byte("0010000000"));// 0xDF13
								aidConfig2.setTerminalFloorLimit(new byte[] { 0x00, 0x00, 0x00, 0x05 });// 0x9f1b
								aidConfig2.setThresholdValueForBiasedRandomSelection(new byte[] { 0x00, 0x00, 0x00,
										(byte) 0x28 });// 0xDF15
								aidConfig2.setMaxTargetPercentageForBiasedRandomSelection(32);// 0xDF16
								aidConfig2.setTargetPercentageForRandomSelection(14);// 0xDF17
								aidConfig2.setDefaultDDOL(ISOUtils.hex2byte("9F3704"));// 0xDF14
								aidConfig2.setOnLinePinCapability(1);// 0xDF18
								aidConfig2.setEcTransLimit(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 });// 0x9F7B
								aidConfig2.setNciccOffLineFloorLimit(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 });// 0xDF19
								aidConfig2.setNciccTransLimit(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 });// 0xDF20
								aidConfig2.setNciccCVMLimit(new byte[] { 0x00, 0x00, 0x00, 0x00, (byte) 0x80, 0x00 });// 0xDF21
								aidConfig2.setEcCapability(0);// 0xDF24
								aidConfig2.setCoreConfigType(2);// 0xDF25
								boolean addAIDResult2 = false;
								if (connectType.equals(Const.ConnectTypeName.K21)) {
									k21EmvInterfaceImpl.initEmvModule(mainActivity);
									addAIDResult2 = k21EmvInterfaceImpl.addAID(aidConfig2);
								} else {
									addAIDResult2 = emvInterfaceImpl.addAID(aidConfig2);
								}
								mainActivity.showMessage("添加AID2结果:" + addAIDResult2, MessageTag.DATA);
								AIDConfig aidConfig3 = new AIDConfig();
								aidConfig3.setAid(ISOUtils.hex2byte("A000000333010103"));// 0x9f06
								aidConfig3.setAppSelectIndicator(0);// 0xDF01
								aidConfig3.setAppVersionNumberTerminal(new byte[] { 0x00, (byte) 0x20 });// 0x9f09
								aidConfig3.setTacDefault(ISOUtils.hex2byte("FC78FCF8F0"));// 0xDF11
								aidConfig3.setTacOnLine(ISOUtils.hex2byte("FC78FCF8F0"));// 0xDF12
								aidConfig3.setTacDenial(ISOUtils.hex2byte("0010000000"));// 0xDF13
								aidConfig3.setTerminalFloorLimit(new byte[] { 0x00, 0x00, 0x00, 0x05 });// 0x9f1b
								aidConfig3.setThresholdValueForBiasedRandomSelection(new byte[] { 0x00, 0x00, 0x00,
										(byte) 0x28 });// 0xDF15
								aidConfig3.setMaxTargetPercentageForBiasedRandomSelection(32);// 0xDF16
								aidConfig3.setTargetPercentageForRandomSelection(14);// 0xDF17
								aidConfig3.setDefaultDDOL(ISOUtils.hex2byte("9F3704"));// 0xDF14
								aidConfig3.setOnLinePinCapability(1);// 0xDF18
								aidConfig3.setEcTransLimit(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 });// 0x9F7B
								aidConfig3.setNciccOffLineFloorLimit(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 });// 0xDF19
								aidConfig3.setNciccTransLimit(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 });// 0xDF20
								aidConfig3.setNciccCVMLimit(new byte[] { 0x00, 0x00, 0x00, 0x00, (byte) 0x80, 0x00 });// 0xDF21
								aidConfig3.setEcCapability(0);// 0xDF24
								aidConfig3.setCoreConfigType(2);// 0xDF25
								boolean addAIDResult3 = false;

								if (connectType.equals(Const.ConnectTypeName.K21)) {
									k21EmvInterfaceImpl.initEmvModule(mainActivity);
									addAIDResult3 = k21EmvInterfaceImpl.addAID(aidConfig3);
								} else {
									addAIDResult3 = emvInterfaceImpl.addAID(aidConfig3);
								}
								mainActivity.showMessage("添加AID3结果:" + addAIDResult3, MessageTag.DATA);
								AIDConfig aidConfig4 = new AIDConfig();
								aidConfig4.setAid(ISOUtils.hex2byte("A000000333010106"));// 0x9f06
								aidConfig4.setAppSelectIndicator(0);// 0xDF01
								aidConfig4.setAppVersionNumberTerminal(new byte[] { 0x00, (byte) 0x20 });// 0x9f09
								aidConfig4.setTacDefault(ISOUtils.hex2byte("FC78FCF8F0"));// 0xDF11
								aidConfig4.setTacOnLine(ISOUtils.hex2byte("FC78FCF8F0"));// 0xDF12
								aidConfig4.setTacDenial(ISOUtils.hex2byte("0010000000"));// 0xDF13
								aidConfig4.setTerminalFloorLimit(new byte[] { 0x00, 0x00, 0x00, 0x05 });// 0x9f1b
								aidConfig4.setThresholdValueForBiasedRandomSelection(new byte[] { 0x00, 0x00, 0x00,
										(byte) 0x28 });// 0xDF15
								aidConfig4.setMaxTargetPercentageForBiasedRandomSelection(32);// 0xDF16
								aidConfig4.setTargetPercentageForRandomSelection(14);// 0xDF17
								aidConfig4.setDefaultDDOL(ISOUtils.hex2byte("9F3704"));// 0xDF14
								aidConfig4.setOnLinePinCapability(1);// 0xDF18
								aidConfig4.setEcTransLimit(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 });// 0x9F7B
								aidConfig4.setNciccOffLineFloorLimit(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 });// 0xDF19
								aidConfig4.setNciccTransLimit(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 });// 0xDF20
								aidConfig4.setNciccCVMLimit(new byte[] { 0x00, 0x00, 0x00, 0x00, (byte) 0x80, 0x00 });// 0xDF21
								aidConfig4.setEcCapability(0);// 0xDF24
								aidConfig4.setCoreConfigType(2);// 0xDF25

								boolean addAIDResult4 = false;
								if (connectType.equals(Const.ConnectTypeName.K21)) {
									k21EmvInterfaceImpl.initEmvModule(mainActivity);
									addAIDResult4 = k21EmvInterfaceImpl.addAID(aidConfig4);
								} else {
									addAIDResult4 = emvInterfaceImpl.addAID(aidConfig4);
								}

								mainActivity.showMessage("添加AID4结果:" + addAIDResult4, MessageTag.DATA);
								mainActivity.processingUnLock();
							} catch (Exception e) {
								mainActivity.showMessage("增加AID:异常！" + e, MessageTag.ERROR);
								mainActivity.processingUnLock();
							}
						}
					}).start();

					break;
				case R.id.btn_emv_get_allaid:
					if (deviceType.equals(Const.DeviceName.AUDIO)) {
						mainActivity.showMessage("音频设备不支持获取AID", MessageTag.ERROR);
					} else {
						new Thread(new Runnable() {

							@Override
							public void run() {
								try {
									mainActivity.processingLock();
									mainActivity.showMessage("开始获取全部的aid列表:", MessageTag.TIP);
									List<AIDConfig> listAIDConfig;
									if (connectType.equals(Const.ConnectTypeName.K21)) {
										k21EmvInterfaceImpl.initEmvModule(mainActivity);
										listAIDConfig = k21EmvInterfaceImpl.fetchAllAID();
									} else {
										listAIDConfig = emvInterfaceImpl.fetchAllAID();
									}
									if (listAIDConfig != null) {
										for (Iterator i = listAIDConfig.iterator(); i.hasNext();) {
											AIDConfig oneAidConfig = (AIDConfig) i.next();
											byte[] aid = oneAidConfig.getAid();
											String aid1 = Dump.getHexDump(aid);
											mainActivity.showMessage("获取的aid:" + aid1, MessageTag.DATA);
										}
										if (listAIDConfig.size() == 0) {
											mainActivity.showMessage("该设备没有aid！请先增加AID", MessageTag.TIP);
										}
									} else {
										mainActivity.showMessage("获取全部的aid列表失败!", MessageTag.ERROR);
									}
									mainActivity.processingUnLock();
								} catch (Exception e) {
									mainActivity.showMessage("获取全部的aid列表异常" + e.getMessage(), MessageTag.ERROR);
									mainActivity.processingUnLock();
								}

							}
						}).start();
					}

					break;
				case R.id.btn_emv_clear_all_aid:
					new Thread(new Runnable() {

						@Override
						public void run() {
							try {
								mainActivity.processingLock();
								mainActivity.showMessage("开始清理全部AID...", MessageTag.TIP);
								boolean clearAllAID = false;
								if (connectType.equals(Const.ConnectTypeName.K21)) {
									k21EmvInterfaceImpl.initEmvModule(mainActivity);
									clearAllAID = k21EmvInterfaceImpl.clearAllAID();
								} else {
									clearAllAID = emvInterfaceImpl.clearAllAID();
								}
								mainActivity.showMessage("清理所有AID结果:" + clearAllAID, MessageTag.DATA);
								mainActivity.processingUnLock();

							} catch (Exception e) {
								mainActivity.showMessage("清理所有AID:" + "异常" + e.getMessage(), MessageTag.ERROR);
								mainActivity.processingUnLock();
							}
						}
					}).start();

					break;
				case R.id.btn_emv_addpk:
					new Thread(new Runnable() {

						@Override
						public void run() {
							try {
								mainActivity.processingLock();
								mainActivity.showMessage("开始添加公钥...", MessageTag.TIP);
								int P9f22_1 = 1;
								keyIndex = P9f22_1;
								byte[] df02_2 = ISOUtils
										.hex2byte("BBE9066D2517511D239C7BFA77884144AE20C7372F515147E8CE6537C54C0A6A4D45F8CA4D290870CDA59F1344EF71D17D3F35D92F3F06778D0D511EC2A7DC4FFEADF4FB1253CE37A7B2B5A3741227BEF72524DA7A2B7B1CB426BEE27BC513B0CB11AB99BC1BC61DF5AC6CC4D831D0848788CD74F6D543AD37C5A2B4C5D5A93B");
								byte[] df04_2 = ISOUtils.hex2byte("000003");
								byte[] df03_2 = ISOUtils.hex2byte("E881E390675D44C2DD81234DCE29C3F5AB2297A0");
								CAPublicKey caKey2 = new CAPublicKey(P9f22_1, 1, 1, df02_2, df04_2, df03_2, "20091231");
								boolean addCAPK2 = false;
								if (connectType.equals(Const.ConnectTypeName.K21)) {
									k21EmvInterfaceImpl.initEmvModule(mainActivity);
									addCAPK2 = k21EmvInterfaceImpl.addCAPublicKey(rid, caKey2);
								} else {
									addCAPK2 = emvInterfaceImpl.addCAPublicKey(rid, caKey2);
								}
								mainActivity.showMessage("添加公钥1结果:" + addCAPK2, MessageTag.DATA);
								mainActivity.showMessage("索引号为:" + P9f22_1, MessageTag.DATA);

								int P9f22_2 = 2;
								keyIndex = P9f22_2;
								byte[] df02 = ISOUtils
										.hex2byte("A3767ABD1B6AA69D7F3FBF28C092DE9ED1E658BA5F0909AF7A1CCD907373B7210FDEB16287BA8E78E1529F443976FD27F991EC67D95E5F4E96B127CAB2396A94D6E45CDA44CA4C4867570D6B07542F8D4BF9FF97975DB9891515E66F525D2B3CBEB6D662BFB6C3F338E93B02142BFC44173A3764C56AADD202075B26DC2F9F7D7AE74BD7D00FD05EE430032663D27A57");
								byte[] df04 = ISOUtils.hex2byte("000003");
								byte[] df03 = ISOUtils.hex2byte("03BB335A8549A03B87AB089D006F60852E4B8060");
								CAPublicKey caKey1 = new CAPublicKey(P9f22_2, 1, 1, df02, df04, df03, "20141231");
								boolean addCAPK1 = false;
								if (connectType.equals(Const.ConnectTypeName.K21)) {
									k21EmvInterfaceImpl.initEmvModule(mainActivity);
									addCAPK1 = k21EmvInterfaceImpl.addCAPublicKey(rid, caKey1);
								} else {
									addCAPK1 = emvInterfaceImpl.addCAPublicKey(rid, caKey1);
								}
								mainActivity.showMessage("添加公钥2结果:" + addCAPK1, MessageTag.DATA);
								mainActivity.showMessage("索引号为:" + P9f22_2, MessageTag.DATA);

								int P9f22_3 = 3;
								keyIndex = P9f22_3;
								byte[] df02_3 = ISOUtils
										.hex2byte("B0627DEE87864F9C18C13B9A1F025448BF13C58380C91F4CEBA9F9BCB214FF8414E9B59D6ABA10F941C7331768F47B2127907D857FA39AAF8CE02045DD01619D689EE731C551159BE7EB2D51A372FF56B556E5CB2FDE36E23073A44CA215D6C26CA68847B388E39520E0026E62294B557D6470440CA0AEFC9438C923AEC9B2098D6D3A1AF5E8B1DE36F4B53040109D89B77CAFAF70C26C601ABDF59EEC0FDC8A99089140CD2E817E335175B03B7AA33D");
								byte[] df04_3 = ISOUtils.hex2byte("000003");
								byte[] df03_3 = ISOUtils.hex2byte("87F0CD7C0E86F38F89A66F8C47071A8B88586F26");
								CAPublicKey caKey3 = new CAPublicKey(P9f22_3, 1, 1, df02_3, df04_3, df03_3, "20171231");
								boolean addCAPK3 = false;
								if (connectType.equals(Const.ConnectTypeName.K21)) {
									k21EmvInterfaceImpl.initEmvModule(mainActivity);
									addCAPK3 = k21EmvInterfaceImpl.addCAPublicKey(rid, caKey3);
								} else {
									addCAPK3 = emvInterfaceImpl.addCAPublicKey(rid, caKey3);
								}

								mainActivity.showMessage("添加公钥3结果:" + addCAPK3, MessageTag.DATA);
								mainActivity.showMessage("索引号为:" + P9f22_3, MessageTag.DATA);

								int P9f22_4 = 4;
								keyIndex = P9f22_4;
								byte[] df02_4 = ISOUtils
										.hex2byte("BC853E6B5365E89E7EE9317C94B02D0ABB0DBD91C05A224A2554AA29ED9FCB9D86EB9CCBB322A57811F86188AAC7351C72BD9EF196C5A01ACEF7A4EB0D2AD63D9E6AC2E7836547CB1595C68BCBAFD0F6728760F3A7CA7B97301B7E0220184EFC4F653008D93CE098C0D93B45201096D1ADFF4CF1F9FC02AF759DA27CD6DFD6D789B099F16F378B6100334E63F3D35F3251A5EC78693731F5233519CDB380F5AB8C0F02728E91D469ABD0EAE0D93B1CC66CE127B29C7D77441A49D09FCA5D6D9762FC74C31BB506C8BAE3C79AD6C2578775B95956B5370D1D0519E37906B384736233251E8F09AD79DFBE2C6ABFADAC8E4D8624318C27DAF1");
								byte[] df04_4 = ISOUtils.hex2byte("000003");
								byte[] df03_4 = ISOUtils.hex2byte("F527081CF371DD7E1FD4FA414A665036E0F5E6E5");
								CAPublicKey caKey4 = new CAPublicKey(P9f22_4, 1, 1, df02_4, df04_4, df03_4, "20171231");

								boolean addCAPK4 = false;
								if (connectType.equals(Const.ConnectTypeName.K21)) {
									k21EmvInterfaceImpl.initEmvModule(mainActivity);
									addCAPK4 = k21EmvInterfaceImpl.addCAPublicKey(rid, caKey4);
								} else {
									addCAPK4 = emvInterfaceImpl.addCAPublicKey(rid, caKey4);
								}

								mainActivity.showMessage("添加公钥4结果:" + addCAPK4, MessageTag.DATA);
								mainActivity.showMessage("索引号为:" + P9f22_4, MessageTag.DATA);
								mainActivity.processingUnLock();
							} catch (Exception e) {
								mainActivity.showMessage("添加公钥结果:" + "异常" + e.getMessage(), MessageTag.ERROR);
								mainActivity.processingUnLock();
							}

						}
					}).start();

					break;
				case R.id.btn_emv_get_allpk:
					if (deviceType.equals(Const.DeviceName.AUDIO)) {
						mainActivity.showMessage("音频设备不支持获取公钥", MessageTag.ERROR);
					} else {
						new Thread(new Runnable() {

							@Override
							public void run() {
								try {
									mainActivity.processingLock();
									mainActivity.showMessage("开始获取获取公钥列表:", MessageTag.TIP);
									List<CAPublicKey> listCAPublicKey;
									if (connectType.equals(Const.ConnectTypeName.K21)) {
										k21EmvInterfaceImpl.initEmvModule(mainActivity);
										listCAPublicKey = k21EmvInterfaceImpl.fetchAllCAPublicKey();
									} else {
										listCAPublicKey = emvInterfaceImpl.fetchAllCAPublicKey();
									}
									if (listCAPublicKey != null) {
										for (Iterator i = listCAPublicKey.iterator(); i.hasNext();) {
											CAPublicKey caPublicKey = (CAPublicKey) i.next();
											int index = caPublicKey.getIndex();
											byte[] rid = caPublicKey.getRid();
											String rid1 = Dump.getHexDump(rid);
											mainActivity.showMessage("获取的公钥的索引号:" + index, MessageTag.DATA);
											mainActivity.showMessage("获取的公钥的rid:" + rid1, MessageTag.DATA);

										}
										if (listCAPublicKey.size() == 0) {
											mainActivity.showMessage("该设备没有公钥！请先增加公钥", MessageTag.TIP);
										}
									} else {
										mainActivity.showMessage("获取公钥列表:失败", MessageTag.ERROR);
									}
									mainActivity.processingUnLock();
								} catch (Exception e) {
									mainActivity.showMessage("获取公钥列表:异常" + e.getMessage(), MessageTag.ERROR);
									mainActivity.processingUnLock();
								}
							}
						}).start();
					}

					break;
				case R.id.btn_emv_clear_all_pk:
					new Thread(new Runnable() {

						@Override
						public void run() {
							try {
								mainActivity.processingLock();
								mainActivity.showMessage("开始清理全部公钥...", MessageTag.TIP);
								boolean clearAllCAPublicKey = false;
								if (connectType.equals(Const.ConnectTypeName.K21)) {
									k21EmvInterfaceImpl.initEmvModule(mainActivity);
									clearAllCAPublicKey = k21EmvInterfaceImpl.clearAllCAPublicKey();
								} else {
									clearAllCAPublicKey = emvInterfaceImpl.clearAllCAPublicKey();
								}
								mainActivity.showMessage("清理全部公钥结果:" + clearAllCAPublicKey, MessageTag.DATA);
								mainActivity.processingUnLock();
							} catch (Exception e) {
								mainActivity.showMessage("清理全部公钥:" + "异常" + e, MessageTag.ERROR);
								mainActivity.processingUnLock();
							}

						}
					}).start();

					break;

				case R.id.btn_emv_delt_one_pk:
					new Thread(new Runnable() {

						@Override
						public void run() {
							try {
								mainActivity.processingLock();
								mainActivity.showMessage("开始删除一个rid以下某个索引对应公钥...", MessageTag.TIP);
								boolean delPKByRid = false;
								if (connectType.equals(Const.ConnectTypeName.K21)) {
									k21EmvInterfaceImpl.initEmvModule(mainActivity);
									delPKByRid = k21EmvInterfaceImpl.deleteCAPublicKey(rid, keyIndex);
								} else {
									delPKByRid = emvInterfaceImpl.deleteCAPublicKey(rid, keyIndex);
								}
								if (delPKByRid) {
									mainActivity.showMessage("删除一个rid以下某个索引对应公钥:" + "成功", MessageTag.TIP);
								} else {
									mainActivity.showMessage("删除一个rid以下某个索引对应公钥:" + "失败", MessageTag.TIP);

								}
								mainActivity.processingUnLock();
							} catch (Exception e) {
								mainActivity.showMessage("删除一个rid以下某个索引对应公钥:" + "异常" + e, MessageTag.ERROR);
								mainActivity.processingUnLock();
							}
						}
					}).start();

					break;

				case R.id.btn_emv_delt_one_aid:
					new Thread(new Runnable() {

						@Override
						public void run() {
							try {
								mainActivity.processingLock();
								mainActivity.showMessage("开始删除一个AID...", MessageTag.TIP);
								boolean delAID = false;
								if (connectType.equals(Const.ConnectTypeName.K21)) {
									k21EmvInterfaceImpl.initEmvModule(mainActivity);
									delAID = k21EmvInterfaceImpl.deleteAID(aid);
								} else {
									delAID = emvInterfaceImpl.deleteAID(aid);
								}
								if (delAID) {
									mainActivity.showMessage("删除一个AID:" + "成功", MessageTag.TIP);

								} else {
									mainActivity.showMessage("删除一个AID:" + "失败", MessageTag.TIP);
								}
								mainActivity.processingUnLock();
							} catch (Exception e) {
								mainActivity.showMessage("删除一个AID异常:" + e.getMessage(), MessageTag.ERROR);
								mainActivity.processingUnLock();
							}
						}
					}).start();

					break;
				case R.id.btn_emv_get_acount_mess:
					new Thread(new Runnable() {

						@Override
						public void run() {
							try {
								mainActivity.processingLock();
								Set<Integer> set = new HashSet<Integer>();
								set.add(1);
								set.add(2);
								set.add(3);
								EmvCardInfo emvCardInfo;
								if (connectType.equals(Const.ConnectTypeName.K21)) {
									k21EmvInterfaceImpl.initEmvModule(mainActivity);
									emvCardInfo = k21EmvInterfaceImpl.getAccountInfo(set);
								} else {
									emvCardInfo = emvInterfaceImpl.getAccountInfo(set);
								}

								String cardMo = emvCardInfo.getCardNo();
								String cardSecuenceNumber = emvCardInfo.getCard_sequence_number();
								String cardExpirationData = emvCardInfo.getCardExpirationDate();
								if (emvCardInfo != null) {
									mainActivity.showMessage("获取emv流程下的账户信息 成功!", MessageTag.TIP);
									mainActivity.showMessage("卡号为:" + cardMo, MessageTag.DATA);
									mainActivity.showMessage("卡序列号为:" + cardSecuenceNumber, MessageTag.DATA);
									mainActivity.showMessage("卡有效期为:" + cardExpirationData, MessageTag.DATA);
									mainActivity.processingUnLock();
								}

							} catch (Exception e) {
								mainActivity.showMessage("获取emv流程下的账户信息 异常:" + e + "是否已经进行了EMV消费流程", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}
						}
					}).start();

					break;
				case R.id.btn_emv_get_pboc_log:
					new Thread(new Runnable() {

						@Override
						public void run() {
							try {
								mainActivity.processingLock();
								mainActivity.showMessage("开始获取所有pboc交易日志:", MessageTag.TIP);
								List<PbocTransLog> pbocTransLog;
								if (connectType.equals(Const.ConnectTypeName.K21)) {
									k21EmvInterfaceImpl.initEmvModule(mainActivity);
									pbocTransLog = k21EmvInterfaceImpl.fetchPbocLog();
									if (pbocTransLog != null) {
										mainActivity.showMessage("获取所有pboc交易日志:" + "成功", MessageTag.TIP);
										for (PbocTransLog pbocLog : pbocTransLog) {
											byte[] amout = pbocLog.getTradeAmount();
											byte[] merchantName = pbocLog.getMerchantName();
											byte[] tradeAmount = pbocLog.getTradeAmount();
											byte[] tradeData = pbocLog.getTradeDate();
											String amountString = Dump.getHexDump(amout);
											String merchantName1 = Dump.getHexDump(merchantName);
											String tradeAmount1 = Dump.getHexDump(tradeAmount);
											String tradeData1 = Dump.getHexDump(tradeData);
											mainActivity.showMessage("获取的pboc交易的金额:" + amountString, MessageTag.DATA);
											mainActivity
													.showMessage("获取的pboc交易的商户名称:" + merchantName1, MessageTag.DATA);
											mainActivity.showMessage("获取的pboc交易的交易金额" + tradeAmount1, MessageTag.DATA);
											mainActivity.showMessage("获取的pboc交易的日期:" + tradeData1, MessageTag.DATA);
										}

									} else {
										mainActivity.showMessage("获取所有pboc交易日志:" + "失败" + "是否已经进行了ＥＭＶ消费流程",
												MessageTag.ERROR);
									}
								} else {
									mainActivity.showMessage("该设备不支持获取所有pboc交易日志！", MessageTag.DATA);
									
								}
								mainActivity.processingUnLock();
							} catch (Exception e) {
								mainActivity.showMessage("获得所有pboc交易日志 异常:" + e + "是否已经进行了ＥＭＶ消费流程", MessageTag.ERROR);
								mainActivity.processingUnLock();
							}
						}
					}).start();

					break;

				default:
					break;
				}
			}
		} else {
			mainActivity.showMessage("设备未初始化，请先初始化！" + "\r\n", MessageTag.TIP);
		}
	}

}
